// pages/lifeService/managementFee/inviteCode.js
const app = getApp();
var topOpacity = 0;
const QRCode = require('../../../utils/weapp-qrcode.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "访客邀请", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      goUrl:'/pages/lifeService/managementFee/menu'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    codeText: "",
    qrcodeURL:'',
    inviteId:'',
    shareTitle: "",
    shareImg: '',
  },
    //获取微信分享信息
    getShareInfo() {
      var that = this;
      wx.request({
        url: that.data.baseUrl + 'getShareInfo.php',
        method: 'POST',
        data: {
  
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          that.setData({
            shareTitle: res.data.shareTitle,
            shareImg: res.data.shareImg
          });
        },
        fail: function(res) {}
      });
    },
  drawImg(openid) {
    wx.showLoading({
      title: '邀请码生成中...'
    });
    new QRCode('myQrcode', {
      text: openid,
      width: 300, //canvas 画布的宽
      height: 300, //canvas 画布的高
      padding: 0, // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.M, // 二维码可辨识度
      callback: (res) => {
        //工具回调数据
        // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
        wx.hideLoading()
        console.log("生成二维码", res)
        //将图片路劲放入data中，显示在wxml的image标签上
        this.setData({
          qrcodeURL: res.path,
        });
        wx.hideLoading();
      }
    })
  },
  refresh(){
    let that = this;
    var data = {
      inviteId: that.data.inviteId
    }
    //获取生成二维码参数
    wx.request({
      url: that.data.baseUrl + 'api/getApiData.php',
      method: 'POST',
      data: {
        'apiUrl': '/openapi/visitor/v1/qrCode',
        'data': JSON.stringify(data)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.hideLoading();
          that.drawImg(res.data.data.content);
        }else{
          app.showTip(res.data.msg);
        }
      },
      fail: function (res) {
        //调用最终上传数据
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    let id = options.id;
    console.log("options",options)
    
    that.setData({
      inviteId:id
    }) 
    var data = {
      id: id
    }
    var data1 = {
      inviteId: id
    }
    //获取生成二维码参数
    wx.request({
      url: that.data.baseUrl + 'api/getApiData.php',
      method: 'POST',
      data: {
        'apiUrl': '/openapi/visitor/v1/qrCode',
        'data': JSON.stringify(data1)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.hideLoading();
          that.drawImg(res.data.data.content);
        }else{
          app.showTip(res.data.msg);
        }
      },
      fail: function (res) {
        //调用最终上传数据
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
      }
    });
    wx.request({
      url: that.data.baseUrl + 'api/getApiData.php',
      method: 'POST',
      data: {
        'apiUrl': '/openapi/visitor/v1/inviteInfo',
        'data': JSON.stringify(data)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          wx.hideLoading();
          that.setData({
            inviteInfo: res.data.data
          })
        }else{
          app.showTip(res.data.msg);
        }
      },
      fail: function (res) {
        //调用最终上传数据
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
      }
    });
     //调用分享
     that.getShareInfo();
  },
  open(){
    wx.navigateTo({
      url: '/pages/IDCard/openDoor'
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/lifeService/managementFee/inviteCode?id='+that.data.inviteId,
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})